package com.aiiter.house.service.impl;

import java.util.*;

import com.aiiter.common.utils.StringUtils;
import com.aiiter.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aiiter.house.mapper.HousePropertyMapper;
import com.aiiter.house.domain.HouseProperty;
import com.aiiter.house.service.IHousePropertyService;

/**
 * 存储房产的基本信息，包括房主信息Service业务层处理
 * 
 * @author yaocheng
 * @date 2024-08-13
 */
@Service
public class HousePropertyServiceImpl implements IHousePropertyService
{
    @Autowired
    private HousePropertyMapper housePropertyMapper;

    /**
     * 查询存储房产的基本信息，包括房主信息
     * 
     * @param propertyId 存储房产的基本信息，包括房主信息主键
     * @return 存储房产的基本信息，包括房主信息
     */
    @Override
    public HouseProperty selectHousePropertyByPropertyId(Long propertyId)
    {
        return housePropertyMapper.selectHousePropertyByPropertyId(propertyId);
    }

    /**
     * 查询存储房产的基本信息，包括房主信息列表
     * 
     * @param houseProperty 存储房产的基本信息，包括房主信息
     * @return 存储房产的基本信息，包括房主信息
     */
    @Override
    public List<HouseProperty> selectHousePropertyList(HouseProperty houseProperty)
    {
        return housePropertyMapper.selectHousePropertyList(houseProperty);
    }

    /**
     * 查询房产信息树
     * @return
     */
    @Override
    public List<Map<String, String>> selectHousePropertyListTree() {
        HouseProperty houseProperty = new HouseProperty();
        List<HouseProperty> houseProperties = housePropertyMapper.selectHousePropertyList(houseProperty);
        List<Map<String, String>> resultList = new ArrayList<>();
        if (houseProperties != null) {
            for (HouseProperty property : houseProperties) {
                if (property != null && property.getPropertyId() != null && property.getPropertyName() != null) {
                    Map<String, String> propertyMap = new HashMap<>();
                    propertyMap.put("value", String.valueOf(property.getPropertyId()));
                    propertyMap.put("label", property.getPropertyName());
                    resultList.add(propertyMap);
                }
            }
        }
        return resultList;
    }


    /**
     * 新增存储房产的基本信息，包括房主信息
     * 
     * @param houseProperty 存储房产的基本信息，包括房主信息
     * @return 结果
     */
    @Override
    public int insertHouseProperty(HouseProperty houseProperty)
    {
        //        houseProperty.setPropertyId(IdUtils.fastSimpleUUID());
        houseProperty.setCreatedAt(new Date());
        if ("0".equals(houseProperty.getOwnerStatus())){
            houseProperty.setCertificationDate(new Date());
        }
        return housePropertyMapper.insertHouseProperty(houseProperty);
    }

    /**
     * 修改存储房产的基本信息，包括房主信息
     * 
     * @param houseProperty 存储房产的基本信息，包括房主信息
     * @return 结果
     */
    @Override
    public int updateHouseProperty(HouseProperty houseProperty)
    {
        return housePropertyMapper.updateHouseProperty(houseProperty);
    }

    /**
     * 批量删除存储房产的基本信息，包括房主信息
     * 
     * @param propertyIds 需要删除的存储房产的基本信息，包括房主信息主键
     * @return 结果
     */
    @Override
    public int deleteHousePropertyByPropertyIds(String[] propertyIds)
    {
        return housePropertyMapper.deleteHousePropertyByPropertyIds(propertyIds);
    }

    /**
     * 删除存储房产的基本信息，包括房主信息信息
     * 
     * @param propertyId 存储房产的基本信息，包括房主信息主键
     * @return 结果
     */
    @Override
    public int deleteHousePropertyByPropertyId(String propertyId)
    {
        return housePropertyMapper.deleteHousePropertyByPropertyId(propertyId);
    }

    @Override
    public String importUser(List<HouseProperty> housePropertyList, Boolean isUpdateSupport, String operName) {
        StringBuilder message = new StringBuilder();

        for (HouseProperty houseProperty : housePropertyList) {
            if (houseProperty == null || StringUtils.isEmpty(houseProperty.getIdNumber())) {
                message.append("行 " + (housePropertyList.indexOf(houseProperty) + 1) + ": 数据无效\n");
                continue;
            }

            HouseProperty existingProperty = housePropertyMapper.selectHousePropertyByIdNumber(houseProperty.getIdNumber());
            if (existingProperty != null) {
                if (isUpdateSupport) {
                    housePropertyMapper.updateHouseProperty(houseProperty);
                    message.append("行 " + (housePropertyList.indexOf(houseProperty) + 1) + ": 更新成功\n");
                } else {
                    message.append("行 " + (housePropertyList.indexOf(houseProperty) + 1) + ": 已存在，跳过\n");
                }
            } else {
                housePropertyMapper.insertHouseProperty(houseProperty);
                message.append("行 " + (housePropertyList.indexOf(houseProperty) + 1) + ": 插入成功\n");
            }
        }

        return message.toString();
    }


}
